package com.liveneo.adapter.mobile.web.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.google.gson.Gson;
import com.liveneo.adapter.base.entity.PrpmDictType;
import com.liveneo.adapter.base.support.AdapterConstants;
import com.liveneo.adapter.base.support.BaseController;
import com.liveneo.adapter.base.support.LogUtils;
import com.liveneo.adapter.mobile.model.dto.ConstantDto;
import com.liveneo.adapter.mobile.model.response.BranchBankResponse;
import com.liveneo.adapter.mobile.model.response.ConstantResponse;
import com.liveneo.adapter.mobile.service.DictDetailService;
import com.liveneo.adapter.mobile.support.MobileReturnCodeSupport;
import com.liveneo.system.base.utils.EncryptUtil;
import com.liveneo.system.base.utils.JsonUtils;

/**
 * @Description: 银行开发相关接口
 * @return TODO
 * @author liveneo-shuqw
 * @date 2016年6月14日 下午3:11:47
 * Copyright: Copyright (c) 2015 
 * Company:北京朗泰
 * @version 3.0
 */
@Controller
@RequestMapping("/mobile")
public class MobileConstantController extends BaseController {
    @Autowired
    private DictDetailService dictService;

    /**
      * @Title: 查询开户分行接口
      * @Description: TODO
      * @param @param data
      * @param @return 设定文件
      * @return String 返回类型
      * @throws
     */
    @RequestMapping("/getBranchBank.action")
    @ResponseBody
    public String getBranchBank(@ModelAttribute(AdapterConstants.MOBILE_REQUEST_PARAM_NAME) String data) {
        String result = null;
        BranchBankResponse response = null;
        try {
            List<PrpmDictType> dtypeList = dictService.selectDictType();
            Map<String, List<ConstantDto>> map = new HashMap<String, List<ConstantDto>>();
            boolean isChild = false;
            for (PrpmDictType pt : dtypeList) {
                // 选择二级
                if ("catastrophe".equals(pt.getDtCode()) || "cityInfo".equals(pt.getDtCode())) {
                    isChild = true;
                }
                map.put(pt.getDtCode(), dictService.selectDictInfo(pt.getDictTypeId(), isChild));
            }
            System.out.println(new Gson().toJson(map));
            // 待调用核心
            response = MobileReturnCodeSupport.buildReponse(MobileReturnCodeSupport.CODE_SUCCESS,MobileReturnCodeSupport.SYS_CODE, null, BranchBankResponse.class);
        } catch (Exception e) {
            LogUtils.error("查询开户分行系统异常,错误消息：", e);
            response = MobileReturnCodeSupport.buildReponse(MobileReturnCodeSupport.CODE_INTERNAL_ERROR,MobileReturnCodeSupport.SYS_CODE, null, BranchBankResponse.class);
        }
        result = JsonUtils.toJson(response);
        LogUtils.info("拍照查勘上传备注信息,响应结果：" + result);
        // 加密返回
        result = EncryptUtil.encodeBase64(result);
        return result;
    }

    /**
      * @Title: 获取系统字典
      * @Description: TODO
      * @param @return 设定文件
      * @return String 返回类型
      * @throws
     */
    @RequestMapping("/getConstant.action")
    @ResponseBody
    public String getConstant(@ModelAttribute(AdapterConstants.MOBILE_REQUEST_PARAM_NAME) String data) {
        String result = null;
        ConstantResponse response = null;
        try {
            List<PrpmDictType> dtypeList = dictService.selectDictType();
            Map<String, List<ConstantDto>> map = new HashMap<String, List<ConstantDto>>();
            boolean isChild = false;
            for (PrpmDictType pt : dtypeList) {
                // 选择二级
                if ("catastrophe".equals(pt.getDtCode()) || "cityInfo".equals(pt.getDtCode())) {
                    isChild = true;
                }
                map.put(pt.getDtCode(), dictService.selectDictInfo(pt.getDictTypeId(), isChild));
            }
            response = (ConstantResponse) MobileReturnCodeSupport.buildBaseReponse(MobileReturnCodeSupport.CODE_SUCCESS, null, "ConstantResponse");
            response.setConstantMap(map);
        } catch (Exception e) {
            LogUtils.error("查询系统字典异常,错误消息：", e);
            response = (ConstantResponse) MobileReturnCodeSupport.buildBaseReponse(MobileReturnCodeSupport.CODE_INTERNAL_ERROR, null, "ConstantResponse");
        }
        // 结果
        result = JsonUtils.toJson(response);
        LogUtils.info("查询系统字典信息,响应结果：" + result);
        // 加密返回
        result = EncryptUtil.encodeBase64(result);
        return result;
    }
}
